package cn.lic.tool.poi.excel.function.impl;

import org.apache.poi.ss.usermodel.Cell;

import com.alibaba.fastjson.JSONObject;

import cn.lic.tool.poi.excel.ParserUtils;
import cn.lic.tool.poi.excel.function.AbstractFunction;
import cn.lic.tool.poi.excel.function.IFunction;
import cn.lic.tool.poi.excel.function.IFunctionArgument;
import cn.lic.tool.poi.excel.function.IValue;
import cn.lic.tool.poi.excel.function.Range;

/**
 * 接收三个个参数, 如果第一个参数为true返回第二个参数,否则返回第三个参数
 * @author Administrator
 *
 */
public class IF extends AbstractFunction {
	public IF() {}
	public IF(IFunctionArgument[] args) {
		 this.args = args;
	}

	@Override
	public IFunction build(IFunctionArgument[] args) {
		return new IF(args);
	}
	
	@Override
	public IValue execute(JSONObject ctx) {
		IValue v = this.getArg(0, ctx);
		boolean arg0 = v.castValue(IValue.Type.BOOL);
		IValue arg1 = this.getArg(1, ctx);
		IValue arg2 = this.getArg(2, ctx);
		return arg0 ? arg1 : arg2;
	}

	@Override
	public Range execute(JSONObject ctx, Cell cell) {
		IValue rs = this.execute(ctx);
		ParserUtils.setCellValue(cell, rs);
		return null;
	}
}
